﻿//编辑窗体js
(function ($) {
    var _fnCompleteProcess = function (icon, msg, fnCallback) {
        layer.alert(msg, icon, function () {
            if (fnCallback) fnCallback();
            layer.closeAll();
        });
    };
    $.fn.extend({
        //1.成功后执行回调 2.提交前数据验证 3.弹出框显示（不关闭）
        bindSubmit: function (fnSuccessCallback, fnBeforeSubmit, bLayerShow, fnError) {
            $.validator.defaults.ignore = 'input:hidden';
            var $btn = $(this);
            $btn.click(function () {
                if (fnBeforeSubmit) {
                    if (!fnBeforeSubmit()) {
                        return false;
                    }
                }

                var $form = $btn.parents('form:first');
                if (!$form.valid()) {
                    return false;
                }

                var oldHtml = $btn.html();
                var newHtml = $btn.html().replace('提交', '稍后...').replace('icon-ok', 'icon-spinner icon-spin');
                $btn.addClass('disabled ').html(newHtml);

                //超时时间
                var timeout = 15 * 1000;
                var bodyWidth = $('body').width();
                $('#divProgress').css({ width: 0, left: 0 }).show().animate({ width: bodyWidth }, timeout);

                $form.ajaxSubmit({
                    dataType: 'json',
                    timeout: timeout,
                    success: function (data) {
                        if (data.IsSuccess) {

                            $('#divProgress').stop().animate({ width: bodyWidth }, 'slow').animate({ left: bodyWidth }, 'slow');

                            if (!data.Data) data.Data = '执行成功。';

                            if (fnSuccessCallback) {
                                fnSuccessCallback(data);
                            }
                            if (data.Message && data.Message.length > 0)
                                data.Data = data.Message;
                            layer.alert(data.Data, 1, function (i) {
                                if (!bLayerShow) {
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);
                                }
                                layer.close(i);
                            });
                        } else {
                            $('#divProgress').stop().animate({ width: bodyWidth }, 'slow').hide('slow');
                            $btn.html(oldHtml).removeClass('disabled ');
                            _fnCompleteProcess(0, data.ResponseError.Message);
                            if (fnError) {
                                fnError(data);
                            } else {
                                _fnCompleteProcess(0, data.ResponseError.Message);
                            }
                        }
                    },
                    error: null,
                    complete: function (XMLHttpRequest, status) { //请求完成后最终执行参数
                        console.log(XMLHttpRequest);
                        console.log(status);
                        if (status != 'success') {
                            var icon = 8;
                            var msg = '脚本错误[' + status + '], 未知数据是否保存成功, 请确认后联系管理员进行BUG修复。';
                            //超时,status还有success,error等值的情况
                            if (status == 'timeout') {
                                msg = '执行超时, 未知数据是否保存成功。如确认数据未保存可重试。';
                            }

                            $('#divProgress').stop().animate({ width: bodyWidth }, 'slow').slideUp('slow');

                            _fnCompleteProcess(icon, msg, function () {
                                $btn.html(oldHtml).removeClass('disabled ');
                            });
                        }
                    }
                });
                return false;
            });
        },
        bindClose: function (fnBeforeSubmit) {
            $(this).click(function () {
                if (fnBeforeSubmit) {
                    if (!fnBeforeSubmit()) {
                        return false;
                    }
                }
                //获取当前窗口索引
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            });
        }
    });
})(jQuery);